﻿Imports System.Windows.Forms

<Serializable()> Public Class CircleToolV3
    Inherits BaseTool




    Dim p1 As New Drawing.Point
    Dim p2 As New Drawing.Point
    Dim p3 As Drawing.Point '可以用作辅助第一点
    Dim p4 As Drawing.Point '可以用作辅助第二点
    Dim rOld As Double = -1 '作为前一个半径值


    Dim pt As New List(Of Drawing.Point)
    Dim ptOld As New List(Of Drawing.Point)



    '兼容win7的新算法
    Dim m_Handler As Integer

    Dim tx, ty As Double





    Public Overrides Sub mouseDblClick(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Public Overrides Sub mouseDown(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseDownEvent)

        If e.button = 1 Then



            If Me.OperShapeProperty Is Nothing Then '第一次绘制



                Me.OperShapeProperty = ShapeFactory.getNewPolyShape()


                Me.OperShapeProperty.P1Property = Me.DownPointProperty
                Me.OperShapeProperty.plist.Add(Me.DownPointProperty)

                p1 = Me.DownPointProperty



                '为圆形的绘制作出准备

                m_Handler = mapwin.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList)

                mapwin.PixelToProj(e.x, e.y, tx, ty)



            Else

                '对于直线来讲，这里的绘制已经结束



                Me.OperShapeProperty.plist.Add(Me.DownPointProperty)


                '清空
                mapwin.ClearDrawing(m_Handler)
                mapwin.Redraw()



                InvokeEndDrawing()
                Me.OperShapeProperty = Nothing


                rOld = -1





            End If

        End If
    End Sub

    Public Overrides Sub mouseDrag(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseMoveEvent)

    End Sub

    Public Overrides Sub mouseMove(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseMoveEvent)
        '曲线的辅助绘制，我真的想哭啊
        If Me.OperShapeProperty.plist.Count = 1 Then

            p2.X = e.x

            p2.Y = e.y

            Dim r As Double = Math.Sqrt((p2.X - p1.X) ^ 2 + (p2.Y - p1.Y) ^ 2)





            mapwin.ClearDrawing(m_Handler)
            m_Handler = mapwin.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList)



            mapwin.DrawCircleEx(m_Handler, tx, ty, r, 10, False)




            'DrawCircle(rOld)

            'DrawCircle(r)
            'If rOld <> -1 Then
            '    DrawCircleEx(rOld)
            'End If



            'DrawCircleEx(r)





            rOld = r










        End If
    End Sub

    Public Overrides Sub mouseUp(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseUpEvent)

    End Sub

    Public Overrides Sub setself()

    End Sub

    Public Overrides Sub unSet()

    End Sub

    Private Sub DrawCircle(ByVal r As Double)

        Dim pOld As New Drawing.Point
        Dim pOld2 As New Drawing.Point

        pOld = p1
        pOld2 = p1

        For i As Integer = (-1 * Math.Floor(r)) To Math.Floor(r)

            p3.X = p1.X + i
            p3.Y = Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y

            p4.X = p1.X + i
            p4.Y = (-1) * Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y


            ControlPaint.DrawReversibleLine(getScreen(pOld), getScreen(p3), lColor)
            ControlPaint.DrawReversibleLine(getScreen(pOld2), getScreen(p4), lColor)

            pOld = p3
            pOld2 = p4


        Next



        'rOld = r


    End Sub

    Private Sub DrawCircleEx(ByVal r As Double)




        pt.Clear()
        ptOld.Clear()


        For i As Integer = (-1 * Math.Floor(r)) To Math.Floor(r)

            p3.X = p1.X + i
            p3.Y = Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y

            p4.X = p1.X + i
            p4.Y = (-1) * Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y

            pt.Add(p3)
            ptOld.Add(p4)






        Next

        ptOld.Reverse()
        pt.AddRange(ptOld)


        Dim pOld As New Drawing.Point

        pOld = pt.Item(0)

        For Each pTemp As Drawing.Point In pt



            ControlPaint.DrawReversibleLine(getScreen(pOld), getScreen(pTemp), lColor)
            pOld = pTemp

        Next


        ControlPaint.DrawReversibleLine(getScreen(pOld), getScreen(pt.Item(0)), lColor)






    End Sub

    Public Overrides Sub CancelDraw()
        MyBase.CancelDraw()


        '清空
        mapwin.ClearDrawing(m_Handler)
        mapwin.Redraw()

        rOld = -1


    End Sub



End Class
